package com.liuheng.manager.utils;

import com.liuheng.spzx.model.entity.system.SysMenu;

import java.util.ArrayList;
import java.util.List;

public class MenuHelper {
    public static List<SysMenu> getTree(ArrayList<SysMenu> menus){
        ArrayList<SysMenu> trees=new ArrayList<>();
        for (SysMenu menu : menus) {
            if(menu.getParentId()==0)
            {
               trees.add(findMenus(menus,menu));
            }
        }
        return trees;
    }

    private static SysMenu findMenus(ArrayList<SysMenu> menus, SysMenu menu) {
        Long menuId = menu.getId();
        ArrayList<SysMenu> children=new ArrayList<>();
        menu.setChildren(children);
        for (SysMenu sysMenu : menus) {
            if(sysMenu.getParentId().longValue()==menuId.longValue()){
//                children.add(sysMenu);
                menu.getChildren().add(findMenus(menus,sysMenu));
            }
        }
         return menu;
    }
}
